<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

include('../includes/db_connect.php');
include('../functions/fonctions.php');

//OPERATIONS DE CREATION D'UN service
//On verifie si le formulaire a ete envoye
    if(isset($_POST['submit']))
    {   
        if(get_magic_quotes_gpc())
        {
            $division = stripslashes(htmlspecialchars($_POST['division']));
            $service = stripslashes(htmlspecialchars($_POST['service']));
        } else {
            $division = mysql_real_escape_string(htmlspecialchars($_POST['division']));
            $service = mysql_real_escape_string(htmlspecialchars($_POST['service']));
        }
        if ($division!=NULL && $service!=NULL ){
            $req = mysql_query('SELECT * FROM service WHERE service_label = "'.$service.'"  AND division_id = "'.$division.'"');
            $dn = @mysql_fetch_array($req);
            if ($dn['service_label'] == $service and mysql_num_rows($req)>0){
                //Sinon, on indique que le champ ne doit pas être vide
                         $form = true;
                         $_SESSION['error_message'] = '<strong>Désolé!!!</strong> Cette dénomination existe déjà.';
            } else {
                $result = mysql_insert('service', 
                        array( 
                            'service_label' => $service, 
                            'division_id'=>$division, 
                            'service_date_add'=>date('Y-m-d h:i:s'),
                            'service_date_upd'=>date('Y-m-d h:i:s')
                        )
                    );
                $form = false;
                $_SESSION['success_message'] = '<strong>Alerte!!!</strong> Le service a été ajouté avec succès.<br />';
                unset($service);
                unset($division);
            }
        } else {
            //Sinon, on indique que le champ ne doit pas être vide
       
            $form = true;
            $_SESSION['error_message'] = '<strong>Désolé!!!</strong> Aucun des champs ne doit être vide.';
        }
    } 
$divisions = mysql_query('
        SELECT d.*, m.* 
        FROM division d 
            LEFT JOIN managment m  ON d.managment_id = m.managment_id 
        WHERE d.division_deleted="0"');
$services = mysql_query('
        SELECT s.*, d.*, m.* 
        FROM service s 
            LEFT JOIN division d  ON s.division_id = d.division_id
            LEFT JOIN managment m  ON d.managment_id = m.managment_id 
        WHERE s.service_deleted="0"');
//LE TITRE DE LA PAGE
$titre = 'SERVICES ET BUREAUX';
//MENU ACTIF
$menu = 'organization';
//SOUS MENU ACTIF
$sous_menu = 'service';
//LE TITRE DE L'OPERATION
$page_header_title = array('h4'=>'Services/Bureaux','small'=>'Les services de votre organigramme');
//LE HAUT DE LA PAGE
include('../layout/header.php');
?>

      <!-- Le contenu de notre page -->
       
      
      <div class="row">
        <div class="col-md-6">
            <div class="panel panel-success">
                <div class="panel-heading">
                    <h3 class="panel-title">Formulaire de creation d'un service ou d'un bureau</h3>
                </div>
                <div class="panel-body">
                     <!--Début du formulaire-->
                    <form class="form-horizontal" role="form" method="post" action="">
                        <div class="form-group">
                            <label for="managment" class="col-sm-5 control-label">Division</label>
                            <div class="col-sm-7">
                                <select class="form-control" id="division" name="division">
                                    <?php if (@mysql_num_rows($divisions) == 0){?>
                                        <option value="">Ajoutez des divisions</option>
                                    <?php } else { ?>
                                        <option value="">--Selection des divisions--</option>
                                    <?php while ($item = mysql_fetch_assoc($divisions)) {?>
                                        <option <?php echo isset($division) ? (($division == $item['division_id']) ? 'selected' : '' ) : '';?> value="<?php echo $item['division_id']; ?>"><?php echo $item['managment_label']; ?> - <?php echo $item['division_label']; ?></option>
                                        <?php } ?>                                        
                                    <?php  } ?>
                                </select> 
                            </div>
                        </div>
                        <div class="form-group">
                          <label for="service" class="col-sm-5 control-label">Dénomination</label>
                          <div class="col-sm-7">
                            <input type="text" class="form-control" id="service" name="service" placeholder="Par ex. Chargé de la clientèle" autocomplete="off" value="<?php echo isset($service) ? $service : ''; ?>">
                          </div>
                        </div>
                        <div class="form-group">
                          <div class="col-sm-offset-5 col-sm-7">
                            <button type="submit" class="btn btn-default" name="submit" value="1"><i class="glyphicon glyphicon-floppy-saved"></i> Enregistrer</button>
                          </div>
                        </div>
                      </form>
                    <!--Fin du formulaire-->
                </div>
                <div class="panel-footer"></div>
            </div>
            </div>
            <div class="col-md-6">
                <div class="panel panel-success">
                    <div class="panel-heading">
                        <h3 class="panel-title">Liste des services</h3>
                    </div>
                    <div class="panel-body">
                        <table class="table table-hover">
                          <thead>
                            <tr>
                              <th>#</th>
                              <th>Direction</th>
                              <th>Division</th>
                              <th>Dénomination</th>
                              <th>Actions</th>
                            </tr>
                          </thead>
                          <tbody>
                            <?php if(@mysql_num_rows($services)>0){
                                while ($service= mysql_fetch_array($services)){ ?>
                                    <tr>
                                      <td><?php echo $service['service_id'] ?></td>
                                      <td><?php echo $service['managment_label'] ?></td>
                                      <td><?php echo $service['division_label'] ?></td>
                                      <td><?php echo $service['service_label'] ?></td>
                                      <td>
                                            <a type="button" class="btn btn-default btn-xs" data-widget="delete" data-toggle="tooltip" data-original-title="Delete" href="<?php ?>"><i class="glyphicon glyphicon-trash"></i></a>
                                            <a type="button" class="btn btn-default btn-xs" href="<?php ?>"><i class="glyphicon glyphicon-edit"></i></a>
                                      </td>
                                    </tr>
                            <?php    }
                            } else { ?>
                                <tr>
                                  <td colspan="4">Pas de division pour l'instant</td>
                                </tr>
                            <?php }?>                       

                          </tbody>
                        </table>
                    </div>
                    <div class="panel-footer">
                        <ul class="pagination pagination-sm">
                            <li><a href="#">«</a></li>
                            <li><a href="#">1</a></li>
                            <li><a href="#">2</a></li>
                            <li><a href="#">»</a></li>
                          </ul>
                    </div>
                </div>
            </div>            
        </div>
    </div>
          

      
      <!-- La fin du contenu de notre page -->
<?php 
    include('../layout/footer.php');
?>
    <!--Ecrire les scripts jQuery ici-->
  </body>
</html>


